<html>
  <head>
    <title>Conditional Focus Test - Capturing Page</title>
    <link rel="icon" href="data:," />
    <script>
      "use strict";

      let mediaStreamKeepAlive;  // Keeps the capture alive.

      // Calls getDisplayMedia().
      // Calling with |busyWaitMs| set to a positive value leads to hogging
      // the CPU for that long *before* calling focus().
      // Then:
      // * If (focusCallValue == ''), don't call focus().
      // * If (focusCallValue != ''), call focus(focusCallValue).
      // * If |onCorrectMicrotask|, focus() is called on the microtask
      //   on which getDisplayMedia's Promise was resolved. Otherwise, it is
      //   scheduled to be called in a later task.
      async function captureOtherTab(busyWaitMs, focusCallValue, onCorrectMicrotask) {
        try {
          const mediaStream = await navigator.mediaDevices.getDisplayMedia();
          if (onCorrectMicrotask) {
            handleCaptureSuccess(mediaStream, busyWaitMs, focusCallValue);
          } else {
            setTimeout(handleCaptureSuccess, 0, mediaStream, busyWaitMs, focusCallValue);
          }
        } catch (error) {
          window.domAutomationController.send("capture-failure");
        }
      }

      function busyWait(durationMs) {
        const targetTime = new Date().getTime() + durationMs;
        while (new Date().getTime() < targetTime) {
          // Busy-wait.
        }
      }

      function handleCaptureSuccess(mediaStream, busyWaitMs, focusCallValue) {
        mediaStreamKeepAlive = mediaStream;

        if (busyWaitMs > 0) {
          busyWait(busyWaitMs);
        }

        if (focusCallValue) {
          const [track] = mediaStream.getVideoTracks();
          try {
            track.focus(focusCallValue);
          } catch (error) {
            window.domAutomationController.send(`${error}`);
          }
        }

        // Allow the microtask to fire before unblocking the test.
        setTimeout(() => {
          window.domAutomationController.send("capture-success");
        });
      }

      function callFocusAndExpectError() {
        const [track] = mediaStreamKeepAlive.getVideoTracks();
        try {
          track.focus("focus-captured-surface");
          window.domAutomationController.send("no-error");
        } catch (error) {
          window.domAutomationController.send(`${error}`);
        }
      }

      async function captureCloneAndFocusClone() {
        try {
          mediaStreamKeepAlive = await navigator.mediaDevices.getDisplayMedia();
          const [track] = mediaStreamKeepAlive.getVideoTracks();
          const clone = track.clone();
          try {
            clone.focus("focus-captured-surface");
            window.domAutomationController.send("no-error");
          } catch (error) {
            window.domAutomationController.send(`${error}`);
          }
        } catch (error) {
          window.domAutomationController.send("capture-failure");
        }
      }
    </script>
  </head>
  <body>
    <h1>Conditional Focus Test - Capturing Page</h1>
  </body>
</html>
